package com.hanrui.android.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.hanrui.android.R;
import com.hanrui.android.activity.message.AuditActivity;
import com.hanrui.android.adapter.ExpandableItemAdapter;
import com.hanrui.android.library.base.BaseFragment;
import com.hanrui.android.model.CountBean;
import com.hanrui.android.model.ExpandableItem;
import com.hanrui.android.model.ExpandableLevel;
import com.hanrui.android.model.MenuListBean;
import com.hanrui.android.model.WorkBean;
import com.hanrui.android.net.contract.message.audit.CountContract;
import com.hanrui.android.net.presenter.message.audit.CountPresenter;
import com.hanrui.android.utils.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 *
 */

public class WorkFragment extends BaseFragment<CountPresenter> implements CountContract.View,View.OnClickListener {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    ArrayList<MultiItemEntity> list;
    ExpandableItemAdapter adapter;

    Banner banner;

    View bannerHeadView;


    TextView msgNum,actNum,tranNum,meetNum;
    RelativeLayout msgRl,actRL,trainRl,meetRl;

    @Override
    protected int getLayoutId() {
        return R.layout.recyclerview_base;
    }

    @Override
    protected void initInjector() {mFragmentComponent.inject(this);}

    @Override
    protected void initView(View view) {
        initBanner();

        mPresenter.getCountsInfo(getActivity());

        list = init();
        adapter = new ExpandableItemAdapter(list, getActivity());
        adapter.setHeaderView(bannerHeadView);
        final GridLayoutManager manager = new GridLayoutManager(getActivity(), 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.getItemViewType(position) == ExpandableItemAdapter.TYPE_LEVEL_1 ? 1 : manager.getSpanCount();
            }
        });
        mRecyclerview.setAdapter(adapter);
        mRecyclerview.setLayoutManager(manager);
        adapter.expandAll();
//        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
//                if (baseQuickAdapter.getItemViewType(i) == ExpandableItemAdapter.TYPE_LEVEL_1) {
//
//                }
//            }
//        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private ArrayList<MultiItemEntity> init() {
        ArrayList<MultiItemEntity> res = new ArrayList<>();
            MenuListBean bean = getMenuList();
            //001
            if (null != bean.getHy() && bean.getHy().size() > 0) {
                ExpandableLevel kq = new ExpandableLevel(bean.getHy().get(0).getParentName());
                for (WorkBean child : bean.getHy()) {
                    kq.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
                }
                res.add(kq);
            }

            //002
            if (null != bean.getDb() && bean.getDb().size() > 0) {
                ExpandableLevel rc = new ExpandableLevel(bean.getDb().get(0).getParentName());
                for (WorkBean child : bean.getDb()) {
                    rc.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
                }
                res.add(rc);
            }

            //003
            if (null != bean.getWd() && bean.getWd().size() > 0) {
                ExpandableLevel hy = new ExpandableLevel(bean.getWd().get(0).getParentName());
                for (WorkBean child : bean.getWd()) {
                    hy.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
                }
                res.add(hy);
            }

            //004
            if (null != bean.getDsh() && bean.getDsh().size() > 0) {
                ExpandableLevel qt = new ExpandableLevel(bean.getDsh().get(0).getParentName());
                for (WorkBean child : bean.getDsh()) {
                    qt.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
                }
                res.add(qt);
            }

            //005
            if (null != bean.getJx() && bean.getJx().size() > 0) {
                ExpandableLevel yc = new ExpandableLevel(bean.getJx().get(0).getParentName());
                for (WorkBean child : bean.getJx()) {
                    yc.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
                }
                res.add(yc);
            }

        //006
        if (null != bean.getYg() && bean.getYg().size() > 0) {
            ExpandableLevel yc = new ExpandableLevel(bean.getYg().get(0).getParentName());
            for (WorkBean child : bean.getYg()) {
                yc.addSubItem(new ExpandableItem(child.getParentName(), child.getName(), child.getCode()));
            }
            res.add(yc);
        }
        return res;
    }

    private void initBanner() {

        bannerHeadView = getLayoutInflater().inflate(R.layout.item_banner, (ViewGroup) mRecyclerview.getParent(), false);

        msgRl =  bannerHeadView.findViewById(R.id.msgRl);
        actRL =  bannerHeadView.findViewById(R.id.actRL);
        trainRl =  bannerHeadView.findViewById(R.id.trainRl);
        meetRl =  bannerHeadView.findViewById(R.id.meetRl);
        msgNum = bannerHeadView.findViewById(R.id.msgCount);
        actNum = bannerHeadView.findViewById(R.id.actListSize);
        tranNum = bannerHeadView.findViewById(R.id.trainCount);
        meetNum = bannerHeadView.findViewById(R.id.meetingCount);


        msgRl.setOnClickListener(this);
        actRL.setOnClickListener(this);


        banner = bannerHeadView.findViewById(R.id.banner);

        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.banner1);
        list.add(R.mipmap.banner2);
        list.add(R.mipmap.banner3);
        List<String> tt = new ArrayList<>();
        tt.add("新的一年，邀您共同体验");
        tt.add("轻松接入，即刻体验，一切就在分秒之间");
        tt.add("使创新型企业迈向辉煌 使创业者梦想成真 使合作者共赢");
        banner.setImages(list).setBannerTitles(tt).setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE).setImageLoader(new GlideImageLoader()).start();


    }

    @Override
    public void onStart() {
        super.onStart();
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        banner.stopAutoPlay();
    }

    @Override
    public void getCountsInfo(CountBean data) {

        if (null != data) {

            if (data.getMsgCount() > 99) {
                msgNum.setText("99+");
            		}else {
                msgNum.setText(String.valueOf(data.getMsgCount()));
            }

            if (data.getActListSize() > 99) {
                actNum.setText("99+");
            		}else {
                actNum.setText(String.valueOf(data.getActListSize()));
            }

            if (data.getTrainCount() > 99) {
                tranNum.setText("99+");
            		}else {
                tranNum.setText(String.valueOf(data.getTrainCount()));
            }

            if (data.getMeetingCount() > 99) {
                meetNum.setText("99+");
            		}else {
                meetNum.setText(String.valueOf(data.getMeetingCount()));
            }

        		}

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.msgRl:
                AuditActivity.start(getActivity(),2);
                break;
            case R.id.actRL:
                AuditActivity.start(getActivity(),0);

        }
    }


    public static WorkFragment newInstance() {
        WorkFragment fragment = new WorkFragment();
        return fragment;
    }


    private MenuListBean getMenuList(){

        MenuListBean menuListBean = new MenuListBean();

        List<WorkBean> hyList = new ArrayList<>();
        WorkBean hy1 = new WorkBean("会议管理","会议申请","");
        WorkBean hy2 = new WorkBean("会议管理","我的会议","");
        WorkBean hy3 = new WorkBean("会议管理","会议查看","");
        hyList.add(hy1);
        hyList.add(hy2);
        hyList.add(hy3);

        menuListBean.setHy(hyList);


        List<WorkBean> dbList = new ArrayList<>();
        WorkBean db1 = new WorkBean("督办管理","周会督办","");
        WorkBean db2 = new WorkBean("督办管理","月会督办","");
        WorkBean db3 = new WorkBean("督办管理","三色督办","");
        WorkBean db4 = new WorkBean("督办管理","我的督办","");
        dbList.add(db1);
        dbList.add(db2);
        dbList.add(db3);
        dbList.add(db4);

        menuListBean.setDb(dbList);


        List<WorkBean> wdList = new ArrayList<>();
        WorkBean wd1 = new WorkBean("文档库","我的文档","");
        WorkBean wd2 = new WorkBean("文档库","文档查阅","");
        wdList.add(wd1);
        wdList.add(wd2);

        menuListBean.setWd(wdList);


        List<WorkBean> dshList = new ArrayList<>();
        WorkBean dsh1 = new WorkBean("董事会管理","董事会议","");
        dshList.add(dsh1);

        menuListBean.setDsh(dshList);


        List<WorkBean> jxList = new ArrayList<>();
        WorkBean jx1 = new WorkBean("绩效管理","绩效填报","");
        WorkBean jx2 = new WorkBean("绩效管理","我的绩效","");
        WorkBean jx3 = new WorkBean("绩效管理","绩效查看","");
        jxList.add(jx1);
        jxList.add(jx2);
        jxList.add(jx3);

        menuListBean.setJx(jxList);


        List<WorkBean> ygList = new ArrayList<>();
        WorkBean yg1 = new WorkBean("员工管理","我的档案","");
        WorkBean yg2 = new WorkBean("员工管理","档案查看","");
        ygList.add(yg1);
        ygList.add(yg2);

        menuListBean.setYg(ygList);



        return menuListBean;

    }
}
